package com.yj.lottery_system.controller;

import com.yj.lottery_system.common.pojo.CommonResult;
import com.yj.lottery_system.common.utils.JacksonUtil;
import com.yj.lottery_system.controller.param.DrawPrizeParam;
import com.yj.lottery_system.controller.param.WinningRecordsParam;
import com.yj.lottery_system.controller.result.WinningRecordsResult;
import com.yj.lottery_system.service.IDrawPrizeService;
import com.yj.lottery_system.service.dto.WinningRecordsDTO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class DrawPrizeController {
    private static final Logger log = LoggerFactory.getLogger(DrawPrizeController.class);
    @Resource
    private IDrawPrizeService drawPrizeService;

    /**
     * 异步抽奖接口
     * @param param
     * @return
     */
    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@RequestBody@Valid DrawPrizeParam param){
        //打印日志
        log.info("drawPrize DrawPrizeParam : {}", JacksonUtil.writeValueAsString(param));
        //调用service
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordsResult>> showWinningRecords(@RequestBody@Valid WinningRecordsParam param){
        //打印日志
        log.info("showWinningRecords WinningRecordsParam : {}", JacksonUtil.writeValueAsString(param));
        //调用service
        List<WinningRecordsDTO> winningRecordsDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(convertWinningRecordsResult(winningRecordsDTOList));
    }

    /**
     * showWinningRecords 返回结果转换方法
     * @param winningRecordsDTOList
     * @return
     */
    private List<WinningRecordsResult> convertWinningRecordsResult(List<WinningRecordsDTO> winningRecordsDTOList) {
        if(CollectionUtils.isEmpty(winningRecordsDTOList)) {
            return Arrays.asList();
        }
        List<WinningRecordsResult> winningRecordsResultList =  winningRecordsDTOList.stream()
                .map(winningRecordsDTO -> {
                    WinningRecordsResult winningRecordsResult = new WinningRecordsResult();
                    winningRecordsResult.setWinnerId(winningRecordsDTO.getWinnerId());
                    winningRecordsResult.setWinnerName(winningRecordsDTO.getWinnerName());
                    winningRecordsResult.setPrizeName(winningRecordsDTO.getPrizeName());
                    winningRecordsResult.setPrizeTier(winningRecordsDTO.getPrizeTier().getMessage());
                    winningRecordsResult.setWinningTime(winningRecordsDTO.getWinningTime());
                    return winningRecordsResult;
                }).collect(Collectors.toList());

        return winningRecordsResultList;
    }

}
